package com.futureCloudMOM.core.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.segments.NormalSegmentList;
import com.futureCloudMOM.common.utils.core.QueryWapperDTO;
import com.futureCloudMOM.common.utils.core.QueryWapperUtils;
import com.futureCloudMOM.core.base.service.impl.BaseServiceImpl;
import com.futureCloudMOM.core.domain.BasicMaterialType;
import com.futureCloudMOM.core.service.BasicMaterialTypeService;
import com.futureCloudMOM.core.mapper.BasicMaterialTypeMapper;
import com.futureCloudMOM.framework.config.RedisConfig;
import jdk.nashorn.internal.codegen.ObjectClassGenerator;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.swing.text.html.parser.Entity;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 【Basic_MaterialType】的数据库操作Service实现
 *
 * @author Administrator
 * @since 2023-06-06 15:19:29
 */
@Service
@AllArgsConstructor
public class BasicMaterialTypeServiceImpl extends BaseServiceImpl<BasicMaterialTypeMapper, BasicMaterialType> implements BasicMaterialTypeService {

    private final BasicMaterialTypeMapper basicMaterialTypeMapper;

    /**
     * 前端需要的树结构构建
     *
     * @return
     */
    @Override
    public List<BasicMaterialType> getListByTree(List<QueryWapperDTO> list, BasicMaterialType basicMaterialType) {
        // 条件拼接 有条件时根据条件查询 无条件时从根节点查询
        QueryWrapper<BasicMaterialType> queryWrapper = QueryWapperUtils.wapperJojnt(basicMaterialType, list);
        //if (queryWrapper.getExpression().getNormal().size() == 0) {
        //    queryWrapper.eq("parentId", 0);
        //}
        queryWrapper.eq("deleted", 0);
        queryWrapper.orderByDesc("sysCreateTime");
        List<BasicMaterialType> materialTypes = basicMaterialTypeMapper.selectList(queryWrapper);
        //获取父节点的所有子节点
        for (BasicMaterialType basicMaterialType1 : materialTypes) {
            if (isLeaf(basicMaterialType1)) {
                basicMaterialType1.setChildren(getListByParentId(basicMaterialType1));
            }
        }
        // 测试hutool工具包 总结 不好用
        //List<TreeNode<Long>> treeNodeList = materialTypes.stream().map(this::ClassTransitionByTreeNode).collect(Collectors.toList());
        //List<Tree<Long>> treeList = TreeUtil.build(treeNodeList,0L);
        return materialTypes;
    }


    @Override
    public List<BasicMaterialType> getMaterialTypeList(BasicMaterialType materialType) {
        // 条件拼接 可根据名称或者编码进行数据筛选 有条件时根据条件查询 无条件时从根节点查询
        LambdaQueryWrapper<BasicMaterialType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BasicMaterialType::getDeleted,0);
        if(ObjectUtils.isNotEmpty(materialType.getName()) || ObjectUtils.isNotEmpty(materialType.getCode())){
            queryWrapper.like(ObjectUtils.isNotEmpty(materialType.getName()),BasicMaterialType::getName,materialType.getName());
            queryWrapper.eq(ObjectUtils.isNotEmpty(materialType.getCode()),BasicMaterialType::getCode,materialType.getCode());
        }else{
            queryWrapper.eq(BasicMaterialType::getParentId,0);
        }
        List<BasicMaterialType> materialTypes = basicMaterialTypeMapper.selectList(queryWrapper);
        //获取父节点的所有子节点
        for(BasicMaterialType basicMaterialType : materialTypes){
            if(isLeaf(basicMaterialType)){
                basicMaterialType.setChildren(getListByParentId(basicMaterialType));
            }
        }
        return materialTypes;
    }

    //获取父节点下所有的子节点
    public List<BasicMaterialType> getListByParentId(BasicMaterialType basicMaterialType) {
        List<BasicMaterialType> materialTypes = basicMaterialTypeMapper.selectList(new LambdaQueryWrapper<BasicMaterialType>()
                .eq(BasicMaterialType::getDeleted, 0)
                .eq(BasicMaterialType::getParentId, basicMaterialType.getId()));
        for (BasicMaterialType basicMaterialType1 : materialTypes) {
            if (isLeaf(basicMaterialType1)) {
                basicMaterialType1.setChildren(getListByParentId(basicMaterialType1));
            }
        }
        return materialTypes;
    }

    //判断该节点是否是叶子节点
    public boolean isLeaf(BasicMaterialType basicMaterialType) {
        return basicMaterialTypeMapper.selectList(new LambdaQueryWrapper<BasicMaterialType>()
                .eq(BasicMaterialType::getDeleted, 0)
                .eq(BasicMaterialType::getParentId, basicMaterialType.getId())).size() > 0;
    }

    // 转换TreeNode
    public TreeNode<Long> ClassTransitionByTreeNode(Object t){
        TreeNode<Long> treeNode = new TreeNode<>();
        // 获取对象数据
        JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(t));
        // 获取对象信息
        Class<?> object = t.getClass();
        Field[] field = object.getDeclaredFields();
        // 除了id和父级id外的其他属性
        Map<String,Object> map = new HashMap<>();
        for(Field field1 : field){
            if (field1.getName().equals("id")){
                treeNode.setId(Long.parseLong(jsonObject.get(field1.getName()).toString()));
            }else if (field1.getName().equals("parentId")){
                treeNode.setParentId(Long.parseLong(jsonObject.get(field1.getName()).toString()));
            }else{
                map.put(field1.getName(),jsonObject.get(field1.getName()));
            }
        }
        treeNode.setExtra(map);
        return treeNode;
    }

}
